﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using System.Net;
using System.Net.Mail;
using System.IO;
using System.Globalization;
using System.Collections;
using System.Configuration;

namespace SAP
{
    public class Emailling
    {
        public string GetTemplate_Submit(int RequestID)
        {
            GeneralFunctions gf = new GeneralFunctions();
            string TemplatePath = System.Web.HttpContext.Current.Server.MapPath("~/EmailFactory/SubmitNotice.htm");
            string l_Rs = "";
            try
            { 
                string l_PathTemplate = string.Empty;

                if (TemplatePath.Trim().Equals(string.Empty))
                {
                    l_Rs = string.Format("Template is empty");
                }
                else
                {
                    GeneralFunctions GF = new GeneralFunctions("");
                    DataTable tbl = GF.ObjectSelect("usp_OffHeader_Select", "@hdrID", RequestID.ToString());

                    l_Rs = File.ReadAllText(TemplatePath);
                    l_Rs = l_Rs.Replace("#EmployeeName#", tbl.Rows[0]["ReqName"].ToString());
                    l_Rs = l_Rs.Replace("#ADName#", tbl.Rows[0]["U_UserID"].ToString());
                    l_Rs = l_Rs.Replace("#RequestType#",gf.RequestType( tbl.Rows[0]["ReqType"].ToString()));
                    l_Rs = l_Rs.Replace("#Store#", tbl.Rows[0]["DefaultWhsName"].ToString());
                    l_Rs = l_Rs.Replace("#Link#", ConfigurationManager.AppSettings["PRUrl"].ToString() + "/Popup_Approval.aspx?requestID=" + RequestID.ToString());
                    CultureInfo ivC = new System.Globalization.CultureInfo("es-US");

                    l_Rs = l_Rs.Replace("#DateSubmitted#", String.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(tbl.Rows[0]["DocDate"].ToString()), ivC));
                }
            }
            catch (Exception ex)
            {
                return "";
            }
            return l_Rs;
        }
        public string GetTemplate_Approve(int RequestID)
        {
            GeneralFunctions gf = new GeneralFunctions();
            string TemplatePath = System.Web.HttpContext.Current.Server.MapPath("~/EmailFactory/ApproveNotice.htm");
            string l_Rs = "";
            try
            {
                string l_PathTemplate = string.Empty;

                if (TemplatePath.Trim().Equals(string.Empty))
                {
                    l_Rs = string.Format("Template is empty");
                }
                else
                {
                    GeneralFunctions GF = new GeneralFunctions("");
                    DataTable tbl = GF.ObjectSelect("usp_OffHeader_Select", "@hdrID", RequestID.ToString());

                    l_Rs = File.ReadAllText(TemplatePath);
                    l_Rs = l_Rs.Replace("#EmployeeName#", tbl.Rows[0]["ReqName"].ToString());
                    l_Rs = l_Rs.Replace("#ADName#", tbl.Rows[0]["U_UserID"].ToString());
                    l_Rs = l_Rs.Replace("#RequestType#", gf.RequestType(tbl.Rows[0]["ReqType"].ToString()));
                    l_Rs = l_Rs.Replace("#Store#", tbl.Rows[0]["DefaultWhsName"].ToString());
                    //l_Rs = l_Rs.Replace("#Link#", ConfigurationManager.AppSettings["PRUrl"].ToString() + "/Popup_Approval.aspx?requestID=" + RequestID.ToString());
                    CultureInfo ivC = new System.Globalization.CultureInfo("es-US");

                    l_Rs = l_Rs.Replace("#DateSubmitted#", String.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(tbl.Rows[0]["DocDate"].ToString()), ivC));
                }
            }
            catch (Exception ex)
            {
                return "";
            }
            return l_Rs;
        }
        public string GetTemplate_Reject(int RequestID)
        {
            GeneralFunctions gf = new GeneralFunctions();
            string TemplatePath = System.Web.HttpContext.Current.Server.MapPath("~/EmailFactory/RejectNotice.htm");
            string l_Rs = "";
            try
            {
                string l_PathTemplate = string.Empty;

                if (TemplatePath.Trim().Equals(string.Empty))
                {
                    l_Rs = string.Format("Template is empty");
                }
                else
                {
                    GeneralFunctions GF = new GeneralFunctions("");
                    DataTable tbl = GF.ObjectSelect("usp_OffHeader_Select", "@hdrID", RequestID.ToString());

                    l_Rs = File.ReadAllText(TemplatePath);
                    l_Rs = l_Rs.Replace("#EmployeeName#", tbl.Rows[0]["ReqName"].ToString());
                    l_Rs = l_Rs.Replace("#ADName#", tbl.Rows[0]["U_UserID"].ToString());
                    l_Rs = l_Rs.Replace("#RequestType#", gf.RequestType(tbl.Rows[0]["ReqType"].ToString()));
                    l_Rs = l_Rs.Replace("#Store#", tbl.Rows[0]["DefaultWhsName"].ToString());
                    //l_Rs = l_Rs.Replace("#Link#", ConfigurationManager.AppSettings["PRUrl"].ToString() + "/Popup_Approval.aspx?requestID=" + RequestID.ToString());
                    CultureInfo ivC = new System.Globalization.CultureInfo("es-US");

                    l_Rs = l_Rs.Replace("#DateSubmitted#", String.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(tbl.Rows[0]["DocDate"].ToString()), ivC));
                }
            }
            catch (Exception ex)
            {
                return "";
            }
            return l_Rs;
        }
        public string SendMailNow(string ToEmailList,string Subject,string Template)
        {
            string[] ar= new string[4];
            ar=ConfigurationManager.AppSettings["SMTP"].ToString().Split(';');

            string l_smtpserver = ar[0] ;
            string l_SenderEmail = ar[1];
            string l_pwd = ar[2];
            string l_port = ar[3];

            string l_Rs = "Email Sent!";
            if (ToEmailList.Trim().Equals(string.Empty))
            {
                ToEmailList = l_SenderEmail;
            }
           
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.From = new MailAddress(l_SenderEmail);
            msg.To.Add(ToEmailList);
            msg.Subject = Subject;
            msg.Body = Template;
            msg.IsBodyHtml = true;
            try
            {
                SmtpClient client = new SmtpClient(l_smtpserver,int.Parse(l_port));
                client.EnableSsl = true;
                client.Timeout = 0;
                client.Credentials = new NetworkCredential(l_SenderEmail, l_pwd);
                client.Send(msg);
            }
            catch (SmtpException ex)
            {
                l_Rs = "<span style='color:#CC0033'>Send error:<br /><br />" + ex.Message + "</span>";
                return l_Rs;
            }
            return l_Rs;
        }
    }
}